package com.example.jwt.project.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.example.jwt.common.constant.Constants;
import com.example.jwt.common.core.AjaxResult;
import com.example.jwt.framework.security.service.SysLoginService;
import com.example.jwt.project.sys.entity.SysUser;
import com.example.jwt.project.sys.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 登录控制器
 *
 * @author LiJunYi
 * @date 2022/07/26
 */
@RestController
public class LoginController
{
    @Autowired
    ISysUserService userService;

    @Autowired
    SysLoginService loginService;

    /**
     * 登录方法
     *
     * @param user 用户
     * @return {@link AjaxResult}
     */
    @PostMapping("userLogin")
    public AjaxResult login(SysUser user)
    {
        if (StrUtil.isEmpty(user.getUsername()) || StrUtil.isEmpty(user.getPassword()))
        {
            return AjaxResult.error("用户名或密码未输入!");
        }
        String token = loginService.login(user.getUsername(),user.getPassword());
        return AjaxResult.success(Constants.TOKEN, token);
    }
}
